package com.cloudy.cloudyspider.crawler.processor;

import com.alibaba.fastjson.JSON;
import com.cloudy.cloudspider.search.GsonHelper;
import com.cloudy.cloudspider.search.Indexer;
import com.cloudy.cloudspider.search.LuceneWorkQueue;
import com.cloudy.cloudspider.search.SearchNewsModel;
import com.cloudy.cloudspider.search.SearchNewsResult;
import com.cloudy.cloudspider.search.Searcher;
import com.cloudy.cloudyspider.crawler.common.Md5Utils;
import com.cloudy.cloudyspider.crawler.model.TouTiaoModel;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.io.IOException;
import java.util.List;
import java.util.Set;

/**
 * Created by cloudy.xie on 18/1/12.
 */
public class TouTiaoProcessor implements PageProcessor {
    public static final String URL = "https://www.toutiao.com/api/pc/feed/?category=news_hot&utm_source=toutiao&widen=1&max_behot_time=TIME1" +
            "&max_behot_time_tmp=TIME2&tadrequire=true&as=AS&cp=CP&_signature=-BCwOQAAolIb9bvEjfnpefgQsC";

    private Site site = Site.me()
            .setDomain("www.toutiao.com")
            .setSleepTime(100)
            .setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
    ;

    private LuceneWorkQueue workQueue;
    private TouTiaoProcessor(LuceneWorkQueue workQueue){
        super();
        this.workQueue = workQueue;
    }

    @Override
    public void process(Page page) {
        String json = page.getJson().toString();
        TouTiaoModel model = JSON.parseObject(json.replaceAll("abstract","shortContext"),TouTiaoModel.class);
        Set<SearchNewsModel> set = model.toNewsModel();

//        System.out.println(JSON.toJSON(set));
        workQueue.addTask(set);
    }

    @Override
    public Site getSite() {
        return site;
    }

    private String getAsCp(int time) {
        String as = "479BB4B7254C150";
        String cp = "7E0AC8874BB0985";
        String key = Integer.toHexString(time).toUpperCase();
        String md5Key = Md5Utils.md5(key).toUpperCase();

        char[] keyArr = key.toCharArray();
        char[] md5Arr =md5Key.toCharArray();

        if (key.length() == 8) {
            as="";
            cp="";
            for (int i=0;i<5;i++){
                as += String.valueOf(keyArr[i])+String.valueOf(md5Arr[i]);
                cp += String.valueOf(keyArr[i+3])+String.valueOf(md5Arr[i+3]);
            }
        }

        as = "A1" + as + key.substring(key.length()-3,key.length());
        cp = key.substring(key.length()-3) + cp + "E1";

        return TouTiaoProcessor.URL.replace("AS",as).replace("CP",cp);
    }


    private String getUrl1(int time) {
     return getAsCp(time).replace("TIME1", String.valueOf("0")).replace("TIME2",String.valueOf(time));
    }
    private String getUrl2(int time) {
        return getAsCp(time).replace("TIME1", String.valueOf(time)).replace("TIME2",String.valueOf(time));
    }

    public static void start() throws Exception {

        LuceneWorkQueue workQueue = new LuceneWorkQueue(24);
        TouTiaoProcessor touTiaoProcessor = new TouTiaoProcessor(workQueue);

        int time = (int) (System.currentTimeMillis() / 1000L);

        Spider spider = Spider.create(touTiaoProcessor);
//      spider.addUrl(touTiaoProcessor.getUrl(time));

        for(int i=0;i<24;i++){//1天24小时 没小时 取21条
            spider.addUrl(touTiaoProcessor.getUrl1(time-3600*i));
            spider.addUrl(touTiaoProcessor.getUrl2(time-3600*i));
        }
        spider.thread(4);
        spider.start();
        workQueue.run();
        spider.setExitWhenComplete(true);

    }
}
